package com.lagou.edu.controller;

import com.lagou.edu.model.vo.AccountVO;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @program: stage_1_module_3
 * @description:
 * @author: zhangxiaoyang
 * @date: 2020-07-18 21:07
 **/
@Controller
@RequestMapping("/")
@Validated
public class LoginController {

    private static final String USERNAME = "admin";
    private static final String PASSWORD = "admin";

    @RequestMapping("/toLogin")
    public String toLogin() {
        System.out.println("跳转到登录页面");
        return "login";
    }


    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String login(AccountVO accountVO, HttpSession session, HttpServletResponse response) throws IOException {
        if (USERNAME.equals(accountVO.getUsername()) && PASSWORD.equals(accountVO.getPassword())) {
            System.out.println("登录成功，页面跳转");
            session.setAttribute("username", accountVO.getUsername());
            session.setAttribute("password", accountVO.getPassword());
            return "redirect:/resumes/list";
        }
        System.out.println("账号密码错误");
        return "redirect:/toLogin";
    }
}
